package workflow

import (
	"context"

	"github.com/cloudwego/eino/compose"
)

func BuildRiskChecker(ctx context.Context) (r compose.Runnable[any, any], err error) {
	const (
		outcomeImage         = "outcomeImage"
		riskRules            = "riskRules"
		DocumentTransformer1 = "DocumentTransformer1"
		DocumentTransformer2 = "DocumentTransformer2"
		Indexer1             = "Indexer1"
	)
	g := compose.NewGraph[any, any]()
	outcomeImageKeyOfLoader, err := newLoader(ctx)
	if err != nil {
		return nil, err
	}
	_ = g.AddLoaderNode(outcomeImage, outcomeImageKeyOfLoader)
	riskRulesKeyOfLoader, err := newLoader1(ctx)
	if err != nil {
		return nil, err
	}
	_ = g.AddLoaderNode(riskRules, riskRulesKeyOfLoader)
	documentTransformer1KeyOfDocumentTransformer, err := newDocumentTransformer(ctx)
	if err != nil {
		return nil, err
	}
	_ = g.AddDocumentTransformerNode(DocumentTransformer1, documentTransformer1KeyOfDocumentTransformer)
	documentTransformer2KeyOfDocumentTransformer, err := newDocumentTransformer1(ctx)
	if err != nil {
		return nil, err
	}
	_ = g.AddDocumentTransformerNode(DocumentTransformer2, documentTransformer2KeyOfDocumentTransformer)
	indexer1KeyOfIndexer, err := newIndexer(ctx)
	if err != nil {
		return nil, err
	}
	_ = g.AddIndexerNode(Indexer1, indexer1KeyOfIndexer)
	_ = g.AddEdge(compose.START, outcomeImage)
	_ = g.AddEdge(compose.START, riskRules)
	_ = g.AddEdge(Indexer1, compose.END)
	_ = g.AddEdge(outcomeImage, DocumentTransformer1)
	_ = g.AddEdge(riskRules, DocumentTransformer2)
	_ = g.AddEdge(DocumentTransformer1, Indexer1)
	_ = g.AddEdge(DocumentTransformer2, Indexer1)
	r, err = g.Compile(ctx, compose.WithGraphName("riskChecker"), compose.WithNodeTriggerMode(compose.AllPredecessor))
	if err != nil {
		return nil, err
	}
	return r, err
}
